Skip to main content

Scoring

The fitted model is now used to predict t_2 prices and that is used to score each stock based on prediction error and growth.

First, a new dataframe is made with the Current Price replaced with t_1 prices - this will be used to predict t_2 prices.

x_temp = x_new.copy()
x_temp.loc[:, "Current Price"] = y

The columns for actual and predicted t_1 prices are made and prediction error is calculated as a percentage. Error is 0 if the prediction is less than the actual, because underestimating the stock prices is no problem.

x_new["t_1_price_predicted"] = regressor.predict(x_new)
x_new["t_1_price_actual"] = y

x_new["error"] = x_new.apply(lambda row: max(row['t_1_price_predicted'] - row['t_1_price_actual'], 0), axis=1) * 100 / x_new['t_1_price_actual']

Column for t_2 prices is made by predicting on the new dataframe with t_1 prices as Current Price. The growth of prices is calculated as a percentage.

x_new["t_2_price"] = regressor.predict(x_temp)
x_new["growth"] = (x_new["t_2_price"] - x_new["Current Price"]) * 100 / x_new["Current Price"]

Finally, a score is given to each stock as the difference between growth and error.

x_new["score"] = x_new["growth"] - x_new["error"]

Columns for Graham Number, Altman Z Score and Piotroski Score are added back.

for col in ["Graham", "Altman Z Score", "Piotroski score"]:
x_new[col] = x[col]

Now the dataframe has four different scores - Machine Learning (regression) score, Graham Number, Altman Z Score and Piotroski Score.

There scores will be used to shortlist stocks and allocate budget to the portfolio.